package com.heiye.kv.biz.service.imp;

import com.heiye.kv.biz.domain.dataobject.QuizContentDO;
import com.heiye.kv.biz.enums.ResponseCodeEnum;
import com.heiye.kv.biz.repository.QuizContentRepository;
import com.heiye.kv.biz.service.QuizContentService;
import com.heiye.kv.api.dto.req.AddQuizContentReqDTO;
import com.heiye.kv.api.dto.req.DeleteQuizContentReqDTO;
import com.heiye.kv.api.dto.req.FindQuizContentReqDTO;
import com.heiye.kv.api.dto.rsp.FindQuizContentRspDTO;
import com.heiye.framework.common.exception.BizException;
import com.heiye.framework.common.response.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

/**
 * @author: heiye
 * @date: 2025/03/13 上午10:25
 * @version: v1.0.0
 * @description: 提问内容存储业务
 */
@Slf4j
@Service
public class QuizContentServiceImpl implements QuizContentService {

    @Resource
    private QuizContentRepository quizContentRepository;

    /**
     * 添加提问内容
     *
     * @param addQuizContentReqDTO
     * @return
     */
    @Override
    public Response<?> addQuizContent(AddQuizContentReqDTO addQuizContentReqDTO) {
        // 提问 UUID
        String uuid = addQuizContentReqDTO.getUuid();
        // 提问内容
        String content = addQuizContentReqDTO.getContent();

        // 构建数据库 DO 实体类
        QuizContentDO quizContentDO = QuizContentDO.builder()
                .id(UUID.fromString(uuid))
                .content(content)
                .build();

        // 插入数据
        quizContentRepository.save(quizContentDO);

        return Response.success();
    }

    /**
     * 查询提问内容
     *
     * @param findQuizContentReqDTO
     * @return
     */
    @Override
    public Response<?> findQuizContent(FindQuizContentReqDTO findQuizContentReqDTO) {
        // 提问 UUID
        String uuid = findQuizContentReqDTO.getUuid();
        // 根据笔记 ID 查询提问内容
        Optional<QuizContentDO> optional = quizContentRepository.findById(UUID.fromString(uuid));

        // 若笔记不存在
        if (optional.isEmpty()) {
            throw new BizException(ResponseCodeEnum.QUIZ_CONTENT_NOT_FOUND);
        }

        QuizContentDO quizContentDO = optional.get();
        // 构建反参 DTO
        FindQuizContentRspDTO findQuizContentRspDTO = FindQuizContentRspDTO.builder()
                .uuid(quizContentDO.getId())
                .content(quizContentDO.getContent())
                .build();

        return Response.success(findQuizContentRspDTO);
    }

    /**
     * 删除提问内容
     *
     * @param deleteQuizContentReqDTO
     * @return
     */
    @Override
    public Response<?> deleteQuizContent(DeleteQuizContentReqDTO deleteQuizContentReqDTO) {
        // 提问 UUID
        String uuid = deleteQuizContentReqDTO.getUuid();
        // 删除笔记内容
        quizContentRepository.deleteById(UUID.fromString(uuid));

        return Response.success();
    }
}
